package com.cmcc.passenger.ui.callpaging

import android.os.CountDownTimer
import androidx.lifecycle.*
import com.cmcc.passenger.data.AppRequest
import com.cmcc.passenger.data.entity.OrderDetail
import com.cmcc.shared.model.AppResponse
import com.cmcc.shared.utils.L
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.*

/**
 *
 * @ProjectName:    Taxi Hailing
 * @Package:        com.cmcc.passenger.ui.callpaging
 * @ClassName:      CallOrderViewModel
 * @Description:     java类作用描述
 * @Author:         wangyihao
 * @CreateDate:     2020/5/12 9:50
 * @UpdateUser:     更新者：
 * @UpdateDate:     2020/5/12 9:50
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */
class CallOrderViewModel : ViewModel() {

    val orderDetail = MutableLiveData<OrderDetail>()
    /**
     * 获取订单详情
     */
    fun getOrderDetail(id: String) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = AppRequest.getInstance().getOrderDetail(id)
                if (response.isSuccessful) {
                    val data = response.body()
                    orderDetail.value = data?.data
//                    L.d("data:"+data?.data)
//                    avatarUrl.set(data?.data?.driver?.headPortraitAddress?:"")
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }

    val cancelCall = MutableLiveData<AppResponse<String>>()
    /**
     * 放弃叫车
     * @param id order id
     * @param remark 取消原因
     */
    fun cancelOrder(id : String,remark : String) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = AppRequest.getInstance().cancelOrder(id, remark)
                if (response.isSuccessful) {
                    val data = response.body()
                    cancelCall.value = data
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }

    val confirmCall = MutableLiveData<AppResponse<String>>()
    /**
     * 放弃叫车
     * @param id order id
     * @param remark 取消原因
     */
    fun confirmCall(id : String) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = AppRequest.getInstance().arrivePassengerConfirm(id)
                if (response.isSuccessful) {
                    val data = response.body()
                    confirmCall.value = data
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }

    fun countDown10sFinish(): LiveData<String> =
        liveData {
            repeat(11) {
                delay(1000)
                if (it == 10) {
                    emit("${10 - it}")
                    emit("finish")
                }else {
                    emit("${10 - it}")
                }
            }
        }


    private var finishTimer: CountDownTimer? = null
    var isShowOver = MutableLiveData(false)

    fun doneCountDownTimer(createTime: String) {
        viewModelScope.launch(Dispatchers.Main) {
            val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            val startDate: Date = format.parse(createTime)

            val nowTime = Date()
            val timeGap = startDate.time - nowTime.time
                if (finishTimer == null) {
                    finishTimer = object : CountDownTimer(timeGap, 1000L) {
                        override fun onTick(millisUntilFinished: Long) {

                        }

                        override fun onFinish() {
                            isShowOver.postValue(true)
                        }
                    }.start()
                }
        }
    }

}